package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.dto.product.ProductDto;
import com.zhentao.entity.product.Product;
import com.zhentao.entity.product.ProductDetails;
import com.zhentao.entity.product.ProductSku;
import com.zhentao.mapper.ProductDetailsMapper;
import com.zhentao.mapper.ProductSkuMapper;
import com.zhentao.service.ProductService;
import com.zhentao.mapper.ProductMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【product(商品)】的数据库操作Service实现
* @createDate 2024-09-02 20:32:22
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;

    @Resource
    private ProductDetailsMapper productDetailsMapper;
    /**
     * 商品列表
     * @param productDto
     * @return
     */
    @Override
    public Page<Product> findByPage(ProductDto productDto) {
        Page<Product> page = new Page<>(productDto.getPage(),productDto.getLimit());
        Page<Product> page1 = productMapper.findByPage(page,productDto);
        return page1;
    }
    /**
     * 添加商品
     * @param product
     * @return
     */
    @Override
    @Transactional
    public void saveAdd(Product product) {
        // 保存商品数据
        product.setStatus(0);              // 设置上架状态为0
        product.setAuditStatus(0);         // 设置审核状态为0
        productMapper.save(product);
        // 保存商品sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        for(int i=0,size=productSkuList.size(); i<size; i++) {

            // 获取ProductSku对象
            ProductSku productSku = productSkuList.get(i);
            productSku.setSkuCode(product.getId() + "_" + i);       // 构建skuCode

            productSku.setProductId(product.getId());               // 设置商品id
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            productSku.setSaleNum(0);                               // 设置销量
            productSku.setStatus(0);
            productSkuMapper.save(productSku);                    // 保存数据
        }
        // 保存商品详情数据
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);
    }

    /**
     * 根据ID查询商品信息
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Product findById(Long id) {
        // 根据id查询商品数据
        Product product = productMapper.findById(id);
        // 根据商品的id查询sku数据
        List<ProductSku> productSkuList = productSkuMapper.selectList(Wrappers.<ProductSku>lambdaQuery().eq(ProductSku::getProductId,id).eq(ProductSku::getIsDeleted,0).orderByDesc(ProductSku::getId));
        product.setProductSkuList(productSkuList);
        // 根据商品的id查询商品详情数据
        ProductDetails productDetails = productDetailsMapper.selectOne(Wrappers.<ProductDetails>lambdaQuery().eq(ProductDetails::getProductId,id));
        if (productDetails.getImageUrls()!=null){
            product.setDetailsImageUrls(productDetails.getImageUrls());
        }
        // 返回数据
        return product;
    }

    /**
     * 修改商品
     * @param product
     */
    @Transactional
    @Override
    public void updateByProduct(Product product) {
        product.setUpdateTime(new Date());
        // 修改商品基本数据
        productMapper.updateById(product);
        // 修改商品的sku数据
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSku.setUpdateTime(new Date());
            productSkuMapper.updateById(productSku);
        });
        // 修改商品的详情数据
        ProductDetails productDetails = productDetailsMapper.selectOne(Wrappers.<ProductDetails>lambdaQuery().eq(ProductDetails::getProductId,product.getId()));
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetails.setUpdateTime(new Date());
        productDetailsMapper.updateById(productDetails);
    }

    /**
     * 删除商品信息
     * @param id
     */
    @Transactional
    @Override
    public void deleteById(Long id) {
        productMapper.deleteById(id);                   // 根据id删除商品基本数据
        productSkuMapper.delete(Wrappers.<ProductSku>lambdaQuery().eq(ProductSku::getProductId,id));         // 根据商品id删除商品的sku数据
        productDetailsMapper.delete(Wrappers.<ProductDetails>lambdaQuery().eq(ProductDetails::getProductId,id));     // 根据商品的id删除商品的详情数据
    }

    /**
     * 商品审核
     * @param id
     * @param auditStatus
     */
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = productMapper.selectById(id);
        if(auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        } else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.updateById(product);
    }
    /**
     * 商品上下架
     * @param id
     * @param status
     * @return
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = productMapper.selectById(id);
        if(status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }
}




